import { createApp, watch } from 'vue'
import App from './App.vue'
const app = createApp(App)

import './assets/main.css' // 默认样式

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'  // ElementPlus组件样式
import * as ElementPlusIconsVue from '@element-plus/icons-vue' // 引入所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) { app.component(key, component) } //elementPlus的icon

import { ElMessage } from 'element-plus'
import { router } from './router/index.js'
router.beforeEach((to, from, next) => { // 全局前置守卫
    // let token = window.localStorage.getItem('token')
    // if (token == null) { // 未登录
    // if (document.location.pathname != '/login') { // 未登录且不在登录页面 // 新问题 cms到登录页面后 后退 依旧会触发
    // ElMessage.error('未登录,正在跳转到登录页面'); setTimeout(() => { document.location.pathname = '/login' }, 2000)
    // }
    // else { next() } // 未登录但在登录页面
    // }
    // else { next() } // 已登录
    // console.log(to);
    next()
})

// import HelperAxios from "axios";
// HelperAxios.interceptors.request.use( // 请求拦截
//     function (config) {
//         const token = window.localStorage.getItem('token')
//         // if (token != null) { config.headers.token = token }
//         console.log(config);
//         return config;
//     },
//     function (error) {
//         return Promise.reject(error);
//     }
// );
// HelperAxios.interceptors.response.use( // 响应拦截
//     function (response) {
//         console.log(response);
//         return response;
//     },
//     function (error) {
//         return Promise.reject(error);
//     }
// );
// app.config.globalProperties.$HelperAxios = HelperAxios;

import reqFuncAxios from '/util/HelperAxios.js'
app.config.globalProperties.$reqFuncAxios = reqFuncAxios

import { createPinia, storeToRefs } from 'pinia'
import { piniaMainStore } from '@/store/mainStore.js'
const pinia = createPinia()
app.use(pinia)
const pinaData = piniaMainStore() // 引入主仓库
let { navInfo } = storeToRefs(pinaData) // 变为响应式
// pinaData.getterFunLog

// let permissions = JSON.parse(window.localStorage.getItem('permissions'))
// let subTitle
// watch( // 标签页标题 类似面包屑
//     () => navInfo.value,
//     (newVal, oldVal) => {
//         if (permissions) { permissions = JSON.parse(window.localStorage.getItem('permissions')) } // 若不存在则主动获取一次
//         if (Object.keys(newVal).length != 0) { // 此时处于某一级导航中
//             let mainTitle = permissions.filter(i => { return i.permission_id == newVal.main })[0].permission_name
//             if (newVal.sub) { subTitle = permissions.filter(i => { return i.permission_id == newVal.sub })[0].permission_name }
//             document.title = 'CMS' + ('-' + mainTitle) + (newVal.sub ? '-' + subTitle : '')
//         }
//     },
//     { deep: true, immediate: true }
// )

// import { VueDirective } from '../util/CommonFunctions'
// app.use(VueDirective.install) // 自定义全局指令 // 有问题


import subNav from '@/components/common/subNav.vue'
app.component('subNav', subNav)

app.use(ElementPlus)
app.use(router)
app.mount('#app')